//
//  LOLSegmentView.swift
//  MyLOLSwiftProject
//
//  Created by apple on 2016/12/3.
//  Copyright © 2016年 WangChen. All rights reserved.
//

import UIKit

class LOLSegmentView: UIView {

    var buttonNameArray: [String]?
    var controllerArray: [UIViewController]?
    //记录选中的btn
    var selectedBtn: UIButton?
    
    
    lazy var segmentView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 48.0 * kScreenScanlH))
    lazy var segmentScrollView: UIScrollView = {
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 48.0 * kScreenScanlH, width: kScreenWidth, height: kScreenHeight - 48.0 * kScreenScanlH))
        scrollView.contentSize = CGSize(width: kScreenWidth * CGFloat(self.controllerArray!.count), height: kScreenHeight - 48.0 * kScreenScanlH)
        
        scrollView.isPagingEnabled = true
        scrollView.bounces = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        
        return scrollView
    }()
    
    lazy var underline: UILabel = {
        let line: UILabel = UILabel(frame: CGRect(x: 0, y: 46 * kScreenScanlW, width: kScreenWidth / CGFloat(self.controllerArray!.count), height: 2 * kScreenScanlW))
        line.backgroundColor = UIColor.red
        return line
    }()
    
    lazy var downline: UILabel = {
        let line: UILabel = UILabel(frame: CGRect(x: 0, y: 48 * kScreenScanlW, width: kScreenWidth, height: 0.5 * kScreenScanlH))
        line.backgroundColor = UIColor.gray
        return line
    }()
    
    
    //点击监听
    @objc private func segBtnDidClick(sender: UIButton){
        selectedBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 20 * kScreenScanlH)
        selectedBtn?.isSelected = false
        selectedBtn = sender
        selectedBtn?.isSelected = true
        selectedBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 23 * kScreenScanlH)
        
        var center: CGPoint = self.underline.center
        center = CGPoint(x: kScreenWidth / CGFloat(self.controllerArray!.count * 2) + CGFloat(sender.tag) * kScreenWidth / CGFloat(self.controllerArray!.count), y: center.y)
        
        UIView.animate(withDuration: 0.25) {
            self.underline.center = center
        }
        
        segmentScrollView.setContentOffset(CGPoint(x: CGFloat(sender.tag) * kScreenWidth, y: 0), animated: true)
        
    }
    
    
    
    
    
    
    init(frame: CGRect, btnNameArr: [String], controllerArr: [UIViewController], parentController: UIViewController) {
        
        buttonNameArray = btnNameArr
        controllerArray = controllerArr
        
        super.init(frame: frame)
        
        segmentView.tag = 50
        //添加上面的segment条
        addSubview(segmentView)
        //添加scrollView
        addSubview(segmentScrollView)
        
        //创建VC并添加到scrollView上 创建btn
        for (index,value) in controllerArr.enumerated(){
            let vc = value
            segmentScrollView.addSubview(vc.view)
            vc.view.frame = CGRect(x: kScreenWidth * CGFloat(index), y: 0, width: kScreenWidth, height: kScreenHeight - 48.0 * kScreenScanlH)
            parentController.addChildViewController(vc)
            vc.didMove(toParentViewController: parentController)
            
            let segBtn = getSegBtn(title: btnNameArr[index], tag: index, font: UIFont.systemFont(ofSize: 20 * kScreenScanlW))
            segBtn.frame = CGRect(x: kScreenWidth / CGFloat(controllerArr.count) * CGFloat(index), y: 0, width: kScreenWidth / CGFloat(controllerArr.count), height: 48.0 * kScreenScanlH)
            segBtn.addTarget(self, action: #selector(segBtnDidClick(sender:)), for: .touchUpInside)
            segmentView.addSubview(segBtn)
            
            if segBtn.tag == 0 {
                segBtn.isSelected = true
                selectedBtn = segBtn
                segBtn.titleLabel?.font = UIFont.systemFont(ofSize: 23 * kScreenScanlW)
            }else{
                segBtn.isSelected = false
            }
            
        }
        
        //添加下划线和下边界线
        segmentView.addSubview(underline)
        segmentView.addSubview(downline)
        
    }
    
    
    
    //创建segmentbutton
    private func getSegBtn(title: String, tag: Int, font: UIFont) -> (UIButton){
        let btn = UIButton(type: .custom)
        btn.titleLabel?.font = font
        btn.setTitle(title, for: .normal)
        btn.tag = tag
        btn.setTitleColor(UIColor.gray, for: .normal)
        btn.setTitleColor(UIColor.red, for: .selected)
        return btn
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}



// MARK: - scrollView代理
extension LOLSegmentView: UIScrollViewDelegate{
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        var barCenter = underline.center
        
        UIView.animate(withDuration: 0.25) {
            barCenter.x = kScreenWidth / (CGFloat(self.controllerArray!.count) * 2) + scrollView.contentOffset.x / kScreenWidth * (kScreenWidth / CGFloat(self.controllerArray!.count))
            self.underline.center = barCenter
        }
        
        let button: UIButton = segmentView.viewWithTag(Int(scrollView.contentOffset.x / kScreenWidth)) as! UIButton;
        
        selectedBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 20 * kScreenScanlW)
        selectedBtn?.isSelected = false
        selectedBtn = button
        selectedBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 23 * kScreenScanlW)
        selectedBtn?.isSelected = true
    }
    
    
    
    
    
}
